﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.ApiCommands
{
    public class GetChannelUsers : ICommand
    {
        private readonly string m_commandkey = Radiation.Common.Communication.APIMethods.GET_CHANNEL_USERS;

        public GetChannelUsers()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return null;
        }

        public void execute(string[] commands, Client client)
        {
            if (commands.Length >= 3 &&
                commands[2].Length > 2)
            {
                StringBuilder users = new StringBuilder();
                if (ChannelManager.Instance[commands[2]] != null)
                {
                    foreach (Client username in ChannelManager.Instance[commands[2]].Users)
                        if (!username.Invisible)
                            users.Append(username.Nickname + ";");
                    MessageExecution.sendDataAPI(client, commands[2] + ";" + users.ToString().Substring(0, users.ToString().Length - 1), commands[1]);
                }
                else
                    MessageExecution.sendDataAPI(client, commands[2] + ";", commands[1]);
            }
        }
    }
}
